/**
 * Copyright (c) 2023, iDearHanYu Co., Ltd.
 *
 * SPDX-License-Identifier: 
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-09-21     chao         first version
 */

#include "lvgl.h"
#include "volumio_music.h"
#include <stdint.h>
#include <stdio.h>
#include <string.h>

struct __music_player {
    char title[64];
    char artist[64];
    char album[64];
    enum _source current_source;
    uint8_t play_state;
    uint32_t whole_time; //second
    uint32_t play_time; //second
    uint16_t song_index;
};

static const char *source_string[] = {
    "Bluetooth",
    "Streaming",
    "USB Audio",
    "TEST 1",
    "TEST 2",
    "TEST 3",
};

static struct __music_player music_player; 

/* private functions */
static void test_timer_cb(struct _lv_timer_t *timer);

/***************************************************/
/* USER invoke                                     */
/***************************************************/

enum _source music_player_current_source_get(void)
{
    return music_player.current_source;
}

const char *music_player_source_string_get(int source)
{
    if (source >= SOURCE_MAX)
    {
        LV_LOG_ERROR("source overflow\n");
        return 0;
    }
    return source_string[source];
}

void music_player_title_set(const char *title)
{
    strncpy_s(music_player.title, sizeof(music_player.title), title, strlen(title));
}

const char *music_player_title_get(void)
{
    return (const char *)music_player.title;
}

void music_player_artist_set(const char *artist)
{
    strncpy_s(music_player.artist, sizeof(music_player.artist), artist, strlen(artist));
    volumio_music_player_update(PLAYER_UPDATE_TITLE);
}

const char *music_player_artist_get(void)
{
    return (const char *)music_player.artist;
    volumio_music_player_update(PLAYER_UPDATE_ARTIST);
}

void music_player_album_set(const char *album)
{
    strncpy_s(music_player.album, sizeof(music_player.album), album, strlen(album));
    volumio_music_player_update(PLAYER_UPDATE_ALBUM);
}

const char *music_player_album_get(void)
{
    return (const char *)music_player.album;
}

void music_player_play_state_set(uint8_t state)
{
    music_player.play_state = state ? 1 : 0;
    volumio_music_player_update(PLAYER_UPDATE_PLAY_PAUSE);
}

uint8_t music_player_play_state_get(void)
{
    return music_player.play_state;
}

void music_player_play_time_set(uint32_t time)
{
    music_player.play_time = time;
    volumio_music_player_update(PLAYER_UPDATE_PLAY_TIME);
}

uint32_t music_player_play_time_get(void)
{
    return music_player.play_time;
}

void music_player_whole_time_set(uint32_t time)
{
    music_player.whole_time = time;
    volumio_music_player_update(PLAYER_UPDATE_WHOLE_TIME);
}

uint32_t music_player_whole_time_get(void)
{
    return music_player.whole_time;
}


/***************************************************/
/* UI callback                                     */
/***************************************************/

void music_player_param_init_cb(void)
{
    music_player.current_source = SOURCE_STREAMING;
    music_player.play_state = 0;
    music_player.whole_time = 0;
    music_player.play_time = 0; 
    music_player.song_index = 0;
    lv_timer_t *test_timer = lv_timer_create(test_timer_cb, 1000, &music_player);
}

void music_player_play_pause_cb(void)
{
    if (music_player_play_state_get())
    {
        music_player_play_state_set(0);
    }
    else
    {
        music_player_play_state_set(1);
    }
}

void music_player_play_next_cb(void)
{
    music_player.song_index++;
    if (music_player.song_index >= 10)
    {
        music_player.song_index = 0;
    }
}

void music_player_play_prev_cb(void)
{
    if (music_player.song_index == 0)
    {
        music_player.song_index = 9;
    }
    else
    {
        music_player.song_index--;
    }
}

int music_player_switch_source_cb(enum _source source)
{
    if (source == music_player.current_source) return -1;

    if (source == SOURCE_BT)
    {
        music_player.current_source = SOURCE_BT;
        music_player.play_state = 0;
        music_player_title_set("Waiting for true love");
        music_player_artist_set("The John Smith Band");
        music_player_album_set("Rock - 1997");
    }
    else if (source == SOURCE_STREAMING)
    {
        music_player.current_source = SOURCE_STREAMING;
        music_player.play_state = 0;
        music_player_title_set("Need a Better Future");
        music_player_artist_set("My True Name");
        music_player_album_set("Drum'n bass - 2016");
    }
    else if (source == SOURCE_USB_AUDIO)
    {
        music_player.current_source = SOURCE_USB_AUDIO;
        music_player.play_state = 0;
        music_player_title_set("It happened Yesterday");
        music_player_artist_set("Robotics");
        music_player_album_set("Metal - 2015");
    }
    else
    {
        music_player.current_source = source;
        music_player.play_state = 0;
        music_player_title_set(" ");
        music_player_artist_set(" ");
        music_player_album_set(" ");
    }
    return 0;
}



/* test */
static const char * title_list[] = {
    "Waiting for true love 0123456789abcdefghijklmnopqrstuvwxyz",
    "Need a Better Future",
    "Vibrations",
    "Why now?",
    "Never Look Back",
    "It happened Yesterday",
    "Feeling so High",
    "Go Deeper",
    "Find You There",
    "Until the End",
};

static const char * artist_list[] = {
    "The John Smith Band",
    "My True Name",
    "Robotics",
    "John Smith",
    "My True Name",
    "Robotics",
    "Robotics",
    "Unknown artist",
    "Unknown artist",
    "Unknown artist",
};

static const char * genre_list[] = {
    "Rock - 1997",
    "Drum'n bass - 2016",
    "Psy trance - 2020",
    "Metal - 2015",
    "Metal - 2015",
    "Metal - 2015",
    "Metal - 2015",
    "Metal - 2015",
    "Metal - 2015",
    "Metal - 2015",
};

static const uint32_t time_list[] = {
    1 * 60 + 14,
    2 * 60 + 26,
    1 * 60 + 54,
    2 * 60 + 24,
    2 * 60 + 37,
    3 * 60 + 33,
    1 * 60 + 56,
    3 * 60 + 31,
    2 * 60 + 20,
    2 * 60 + 19,
    2 * 60 + 20,
    2 * 60 + 19,
    2 * 60 + 20,
    2 * 60 + 19,
};

static void test_timer_cb(struct _lv_timer_t *timer)
{
    static uint16_t song_index = 0xFFFF;

    if (music_player_play_state_get())
    {
        if (music_player.play_time < music_player_whole_time_get())
        {
            music_player.play_time++;
            music_player_play_time_set(music_player.play_time);
        }
    }

    if (song_index != music_player.song_index)
    {
        song_index = music_player.song_index;
        music_player_play_state_set(1);
        music_player_title_set(title_list[music_player.song_index]);
        music_player_artist_set(artist_list[music_player.song_index]);
        music_player_album_set(genre_list[music_player.song_index]);        
        music_player_play_time_set(0);
        music_player_whole_time_set(time_list[music_player.song_index]);
    }
}


